<?php
namespace app\home\model;

use think\Config;
use think\Db;
use think\Model;
use think\Session;

class SearchModel extends Model
{
	/**
	*	搜索功能
	*   @param array $data  数据
	*/
	public function getSearch($data)
	{
		if($data){
			$where = [];

			/* 商品名模糊查询 */
			$keyword = empty($data['keyword']) ? '' : $data['keyword'];
			$keyword = trim($keyword);
			if(!empty($keyword)){
				$where['g_name'] = ['like',"%$keyword%"];
			}

			/* 已发布的商品 */
			$where['g_status'] = 2;
			$result = Db::table('sz_goods')->where($where)->select();
			$res  = [];
			$arr  = [];
			$map = [];
			foreach($result as $key=>$val){
				$list[$key] = json_decode($val['g_pic'],true);
				foreach($list as $k1=>$v1){
					$res[$key]['photos'] = $v1['photos'];
					foreach($res[$key]['photos'] as $k2=>$v2){
						$arr['url'] = getPreviewUrl($v2['url']);
					}
				}
				$map[$key]['g_pic'] = $arr;
				$map[$key]['id'] = $val['id'];
				$map[$key]['g_name'] = $val['g_name'];
				$map[$key]['g_description'] = $val['g_description'];
				$map[$key]['g_unit'] = $val['g_unit'];
				$map[$key]['g_cprice'] = $val['g_cprice'];
				$map[$key]['g_oprice'] = $val['g_oprice'];
				$map[$key]['g_type'] = $val['g_type'];

			}
			
			return $map;

		}else{
			return false;
		}
		
	}
}